package com.bjdd.jdy.ui.inspection;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

/**
 * Created by Administrator on 2017/5/2.
 */

public class InspectionPresenter extends InspectionContract.Presenter {
    private List<String> paths;
    private List<File> files;
    private String orderNo;
    private int index = 0;
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
        rxManager.on(Constant.DEL_IMG,msg -> view.delImg((int)msg));
    }

    @Override
    void upLoadImg(String orderNo, Map<String,RequestBody> file) {

        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        L.d("用户id："+carrierId);
        rxManager.add(model.upLoadImg(token,carrierId,orderNo,file)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.upLoadBack();
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            T.show("上传失败");
        }));
    }

    @Override
    void startCompress(String order,List<String> data) {
        showLoadingDialog(false);
        files = new ArrayList<>();
        paths = data;
        index = 0;
        this.orderNo = order;
        CompressImg(paths.get(index));
    }

    @Override
    void CompressImg(String path) {
        File file = new File(path);
        Luban.get(context)
                .load(file)
                .putGear(Luban.THIRD_GEAR)
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(File file) {
                        /*RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),file);
                        headPhoto(requestBody);*/
                        files.add(file);
                        if (files.size() == paths.size()){
                            Map<String, RequestBody> params = new HashMap<>();
                            for (int i=0;i<files.size();i++){
                                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),files.get(i));
                                params.put("photos\";filename=\""+i+"+image.png\"" ,requestBody);
                            }
                            upLoadImg(orderNo,params);
                        }else {
                            index ++;
                            CompressImg(paths.get(index));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show(e.getMessage());
                    }
                }).launch();
    }

}
